﻿using Database;
using HarmonyLib;
using Klei.AI;
using KMod;
using Newtonsoft.Json;
using PeterHan.PLib.Core;
using PeterHan.PLib.Options;
using System;
using System.Collections.Generic;
using TUNING;
using UnityEngine;
using static STRINGS.DUPLICANTS;

namespace Always3Interests
{
    [RestartRequired]
    [JsonObject(MemberSerialization.OptIn)]
    public class Always3InterestsSettings
    {
        [Option("Points when more than 3 interest", "")]
        [Limit(0, 400)]
        [JsonProperty]
        public int pointsWhenMoreThan3Interest { get; set; }

        [Option("Starting level on printing pod", "Set the experience of in game printed dups.")]
        [Limit(0, 10)]
        [JsonProperty]
        public int startingLevelOnPrintingPod { get; set; }

        public Always3InterestsSettings()
        {
            pointsWhenMoreThan3Interest = 200;
            startingLevelOnPrintingPod = 10;
        }
    }

    public class Always3Interests : UserMod2
    {

        public static Always3InterestsSettings Settings;

        public override void OnLoad(Harmony harmony)
        {
            Debug.Log("Always3Interests I execute OnLoad!");
            base.OnLoad(harmony);

            PUtil.InitLibrary();
            new POptions().RegisterOptions(this, typeof(Always3InterestsSettings));
            Settings = POptions.ReadSettings<Always3InterestsSettings>();

            if (Settings == null)
            {
                Settings = new Always3InterestsSettings();
            }

            int[] customAttributes = new int[] {
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest,
                Settings.pointsWhenMoreThan3Interest
            };

            Traverse.Create<DUPLICANTSTATS>().Field<int[]>("APTITUDE_ATTRIBUTE_BONUSES").Value = customAttributes;
            Debug.Log("Always3Interests I execute OnLoad END!");
        }

        public static void ReadSettings()
        {
            Debug.Log("Loading settings");

            Settings = POptions.ReadSettings<Always3InterestsSettings>();
            if (Settings == null)
            {
                Settings = new Always3InterestsSettings();
            }
            Debug.Log("Always3Interests Loading settings END!");
        }
    }

    [HarmonyPatch(typeof(MinionStartingStats), "GenerateAptitudes")]
    public class GenerateAptitudesPatch
    {
        public static void Postfix(MinionStartingStats __instance)
        {
            Debug.Log("Always3Interests GenerateAptitudes!");
            //int num = Always3Interests.Settings.numberOfInterests;

            List<SkillGroup> list = new List<SkillGroup>((IEnumerable<SkillGroup>)Db.Get().SkillGroups.resources);
            Debug.Log("num of all skill: " + list.Count);
            list.RemoveAll((Predicate<SkillGroup>)(match => !match.allowAsAptitude));
            Debug.Log("after remove num of all skill: " + list.Count);
            for (int i = 0; i < list.Count; i++)
            {
                if (__instance.skillAptitudes.ContainsKey(list[i]))
                {
                    Debug.Log("Always3Interests has the SkillGroup!" + list[i].Name);
                    continue;
                }
                __instance.skillAptitudes.Add(list[i], DUPLICANTSTATS.APTITUDE_BONUS);
            }
            Debug.Log("Always3Interests GenerateAptitudes end!");
        }
    }

    [HarmonyPatch(typeof(HeadquartersConfig))]
    [HarmonyPatch("ConfigureBuildingTemplate")]
    public class SkillPointPatch
    {
        public static void Postfix(GameObject go)
        {
            Debug.Log("SkillPointPatch !");
            Always3Interests.ReadSettings();
            int startingLevel = Always3Interests.Settings.startingLevelOnPrintingPod;
            go.AddOrGet<Telepad>().startingSkillPoints = startingLevel;
            Debug.Log("SkillPointPatch end!");
        }
    }


}